<?php if (!defined('SYSTEM_ROOT')) { die('Insufficient Permissions'); }
global $m;
//for($a=0;$a<200;$a++){
//    $m->query("INSERT INTO `".DB_NAME."`.`".DB_PREFIX."record` (`id`,`uid`,`mode`,`money`,`remark`,`date`) VALUE (null,1,'存入',1000,'TEST',1400000000)");
//}

if(isset($_GET['query'])){
    $mode = isset($_GET['mode']) ? addslashes(strip_tags($_GET['mode'])) : '其它';
    $ds = isset($_GET['ds']) && !empty($_GET['ds']) ? (int)addslashes(strip_tags($_GET['ds'])) : 0;
    $de = isset($_GET['de']) && !empty($_GET['de']) ? (int)addslashes(strip_tags($_GET['de'])) : 2200000000;
    $ms = isset($_GET['ms']) && !empty($_GET['ms']) ? (int)addslashes(strip_tags($_GET['ms'])) : 0;
    $me = isset($_GET['me']) && !empty($_GET['me']) ? (int)addslashes(strip_tags($_GET['me'])) : 9999999;
    $kw = isset($_GET['kw']) && !empty($_GET['kw']) ? addslashes(strip_tags($_GET['kw'])) : '';
    $p = isset($_GET['p']) ? (int)addslashes(strip_tags($_GET['p'])) : 0;
    $num = 20;
    $pn = floor(floor($p) * $num);
    global $m;
    $ret = array();
    $x = $m->query("SELECT * FROM `".DB_NAME."`.`".DB_PREFIX."record` WHERE `mode` = '{$mode}' AND `money` >= {$ms} AND `money` <= {$me} AND `date` >= {$ds} AND `date` <= {$de} AND `remark` LIKE '%{$kw}%' ORDER BY `date` DESC Limit {$pn},{$num}");
    $c = $m->fetch_array($m->query("SELECT COUNT(id) AS `id` FROM `".DB_NAME."`.`".DB_PREFIX."record` WHERE `mode` = '{$mode}' AND `money` >= {$ms} AND `money` <= {$me} AND `date` >= {$ds} AND `date` <= {$de} AND `remark` LIKE '%{$kw}%'"));
    while($r = $m->fetch_array($x)){
        $fn = $m->fetch_array($m->query("SELECT `fullname` FROM `".DB_NAME."`.`".DB_PREFIX."users` WHERE `id` = {$r['uid']}"));
        $ret[] = array(
            "name" => $fn['fullname'],
            "money" => $r['money'],
            "mode" => $r['mode'],
            "remark" => $r['remark'],
            "date" => date('Y-m-d H:i:s',$r['date'])
        );
    }
    $result = array(
        "status" => true,
        "r_num" => $c['id'],
        "info" => $ret
    );
    echo json_encode($result);
    die;
}
elseif(isset($_GET['allquery'])){
    $p = isset($_GET['p']) ? $_GET['p'] : 0;
    $num = 20;
    $pn = floor(floor($p) * $num);
    global $m;
    $ret = array();
    $x = $m->query("SELECT * FROM `".DB_NAME."`.`".DB_PREFIX."record` ORDER BY `date` DESC Limit {$pn},{$num}");
    $c = $m->fetch_array($m->query("SELECT COUNT(id) AS `id` FROM `".DB_NAME."`.`".DB_PREFIX."record`"));
    while($r = $m->fetch_array($x)){
        $fn = $m->fetch_array($m->query("SELECT `fullname` FROM `".DB_NAME."`.`".DB_PREFIX."users` WHERE `id` = {$r['uid']}"));
        $ret[] = array(
            "name" => $fn['fullname'],
            "money" => $r['money'],
            "mode" => $r['mode'],
            "remark" => $r['remark'],
            "date" => date('Y-m-d H:i:s',$r['date'])
        );
    }
    $result = array(
        "status" => true,
        "r_num" => $c['id'],
        "info" => $ret
    );
    echo json_encode($result);
    die;
}
?>